<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of wemFtp
 *
 * @author Fulvio
 */
class wemFtp {
    
    private $conn_id;
    private $user;
    private $pass;
    private $host;
    private $port;
    
        
    public function open($host, $port){
        
        $this->host=$host;
        $this->port=$port;
        $this->conn_id = ftp_connect($host,$port);
        
        if ($this->conn_id)
            return $this->conn_id;
        else
            return FALSE;
    }
    
    public function login($user, $pass){
        
        if (!isset($this->user))
            $this->user=$user;
        
        if (!isset($this->pass))
            $this->pass=$pass;
        
        if (ftp_login($this->conn_id,$this->user, $this->pass))
            return true;
        else
            return false;
        
    }
    
    public function setPassv(){
        
        if (ftp_pasv($this->conn_id, true))
            return true;
        else
            return false;
    }
    
    private function connect(){
        
        if ($this->open($this->host,$this->port))
                if ($this->login($this->user, $this->pass))
                        if ($this->setPassv())
                            return true;
        return true;
                         
    }
    
    public function close(){
       ftp_close($this->conn_id); 
    }
    
    public function getList($dir=""){
        
        $tries=0;
        do{
            if ($tries>0)
                $this->connect();
            $list=ftp_nlist($this->conn_id,$dir);
            $tries++;
        }while(!$list and $tries<=3);
        
        return $list; 
    }
    
    public function getMdTime($file){
        $tries=0;
        do{
            if ($tries>0)
                $this->connect();
            $time=ftp_mdtm($this->conn_id, $file);
            $tries++;
        }while($time<=0 and $tries<=3);
        
        return $time; 
    }
    
    public function getFile($locFile,$remFile){
        $tries=0;
        do{
            if ($tries>0)
                $this->connect();
            $file=ftp_get($this->conn_id, $locFile, $remFile, FTP_BINARY);
            $tries++;
        }while(!$file and $tries<=3);
        
        return $file;
    }
    
    public function delFile($file){
        $tries=0;
        do{
            if ($tries>0)
                $this->connect();
            $result=ftp_delete($this->conn_id, $file);
            $tries++;
        }while(!$result and $tries<=3);
        
        return $result;
    }
    
    public function isUp(){
        return $this->getList();
    }
}

?>
